package io.kiki.sba.registry.server.clientInterface.converter;


import io.kiki.sba.registry.core.model.Data;
import io.kiki.sba.registry.core.model.MultiSegmentData;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

@Setter
@Getter
public class LocalDataCenterPushData {

    private SegmentDataCounter localSegmentDatas;

    private Map<String, SegmentDataCounter> remoteSegmentDatas;

    public void from(Map<String, List<Data>> pushData, String localDataCenter, long pushVersion, Predicate<String> pushdataPredicate, Set<String> segmentZones) {

        SegmentDataCounter segmentDataCounter = new SegmentDataCounter(new MultiSegmentData(localDataCenter, pushVersion));
        Map<String, SegmentDataCounter> map = new HashMap<>();

        for (String zone : segmentZones) {
            if (pushdataPredicate.test(zone)) {
                SegmentDataCounter segmentDataCounter1 = map.computeIfAbsent(zone, k -> new SegmentDataCounter(new MultiSegmentData(zone, pushVersion)));
                segmentDataCounter1.put(zone, pushData.get(zone));
            } else {
                segmentDataCounter.put(zone, pushData.get(zone));
            }
        }
        this.localSegmentDatas = segmentDataCounter;
        this.remoteSegmentDatas = map;
    }


}
